<html>
<head><meta charset="utf-8"><title>Trying out musl target · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html">Trying out musl target</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226527848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226527848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226527848">(Feb 16 2021 at 15:34)</a>:</h4>
<p>Hi, noob question. I was trying to statically build with musl. I'm on Debian and have installed musl development packages. I'm following this short guide:<br>
<a href="https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html">https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html</a></p>
<p>I create a new test project and build:</p>
<div class="codehilite"><pre><span></span><code>$ rustc --version
rustc 1.50.0 (cb75ad5db 2021-02-10)

$ cargo new test-musl &amp;&amp; cd test-musl
$ cargo build --target x86_64-unknown-linux-musl
</code></pre></div>
<p>but the compiled binary is dynamically linked. What am I missing?</p>



<a name="226528855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226528855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226528855">(Feb 16 2021 at 15:41)</a>:</h4>
<p>cc <a href="https://github.com/rust-lang/rust/issues/80000">#80000</a> <a href="https://github.com/rust-lang/rust/issues/79624">#79624</a></p>



<a name="226529057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226529057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226529057">(Feb 16 2021 at 15:43)</a>:</h4>
<p>I just made a blank binary hello world project and built with musl, and it's statically linked. Do you have any dependencies that use shared libraries (like SQLite)?</p>



<a name="226529221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226529221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226529221">(Feb 16 2021 at 15:44)</a>:</h4>
<p>in this case it's a simple "hello world" project, so I assume being free from any dependencies</p>



<a name="226529306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226529306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226529306">(Feb 16 2021 at 15:45)</a>:</h4>
<p>Hm, I'm seeing "statically linked" with <code>ldd</code> but "dynamically linked" with <code>file</code></p>



<a name="226529523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226529523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226529523">(Feb 16 2021 at 15:46)</a>:</h4>
<p>The binaries are built PIE which can confuse things like file to misclassify them</p>



<a name="226529554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226529554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226529554">(Feb 16 2021 at 15:47)</a>:</h4>
<p>ldd should be the more robust value</p>



<a name="226529646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226529646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226529646">(Feb 16 2021 at 15:48)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="374396">@hyd-dev</span> suggestion points to the root cause. on issue <a href="https://github.com/rust-lang/rust/issues/79624">#79624</a> I read:</p>
<p><code>x86_64-unknown-linux-musl</code> build target is generating ELF LSB shared object (dynamically linked) while <code>i686-unknown-linux-musl</code> is making ELF LSB executable (statically linked) binary.</p>
<p>which I confirm is what is happening to me, too</p>



<a name="226530026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226530026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226530026">(Feb 16 2021 at 15:50)</a>:</h4>
<p>If I <code>ldd</code> the binary it looks to be actually static</p>
<div class="codehilite"><pre><span></span><code>~/tmp/test-musl$ cargo build --target x86_64-unknown-linux-musl
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
~/tmp/test-musl$ ldd target/x86_64-unknown-linux-musl/debug/test-musl
    statically linked
</code></pre></div>



<a name="226530331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226530331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226530331">(Feb 16 2021 at 15:52)</a>:</h4>
<p>I'll try rebuilding the actual project I wanted to build (which yes <span class="user-mention" data-user-id="218805">@Mark Drobnak</span> has many shared dependencies such as Postgres, OpenSSL, etc.)</p>



<a name="226532726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226532726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226532726">(Feb 16 2021 at 16:04)</a>:</h4>
<p>nope, can't get a statically built binary -_-<br>
I can't build for target <code>i686-unknown-linux-musl</code> because I'd need to install a ton of i686 dependencies</p>



<a name="226533253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226533253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226533253">(Feb 16 2021 at 16:06)</a>:</h4>
<p>I'm not sure if you've already done this, so disregard if so, but you often have to configure the shared dependencies via features to statically link.</p>



<a name="226534202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226534202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226534202">(Feb 16 2021 at 16:11)</a>:</h4>
<p>yes, good point. In this case I only need to tell where the openssl development includes and libs are.<br>
But since the resulting binary is still <strong>dynamically linked</strong>  against openssl:</p>
<div class="codehilite"><pre><span></span><code>$ export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu
$ export OPENSSL_INCLUDE_DIR=/usr/include
$ cargo build --target x86_64-unknown-linux-musl
...
$ ldd target/x86_64-unknown-linux-gnu/debug/my-binary
    linux-vdso.so.1 (0x00007ffe8f78e000)
    libssl.so.1.1 =&gt; /lib/x86_64-linux-gnu/libssl.so.1.1 (0x00007f04758fd000)
    libcrypto.so.1.1 =&gt; /lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007f0475609000)
    libgcc_s.so.1 =&gt; /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f04755ef000)
    libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f04755cd000)
    libm.so.6 =&gt; /lib/x86_64-linux-gnu/libm.so.6 (0x00007f0475489000)
    libdl.so.2 =&gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f0475483000)
    libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f04752bc000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f04770a1000)
</code></pre></div>
<p>I suspect that I'm doing something wrong and it silently fails</p>



<a name="226534916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226534916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226534916">(Feb 16 2021 at 16:15)</a>:</h4>
<blockquote>
<p><code>export OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu</code></p>
</blockquote>
<p>I think that would link the openssl built with glibc dynamically.</p>



<a name="226535346"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226535346" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226535346">(Feb 16 2021 at 16:18)</a>:</h4>
<p>There's a <a href="https://github.com/sfackler/rust-openssl/blob/d7c36c63af66b5272b0b64a803983e5fa4fc3af6/openssl-sys/Cargo.toml#L15"><code>vendored</code> feature</a> in <code>openssl</code>/<code>openssl-sys</code> crate.</p>



<a name="226535805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226535805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226535805">(Feb 16 2021 at 16:20)</a>:</h4>
<p>That would build openssl from source (<a href="https://docs.rs/openssl/0.10.32/openssl/#vendored">https://docs.rs/openssl/0.10.32/openssl/#vendored</a>), but I have never tried that.</p>



<a name="226536998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226536998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226536998">(Feb 16 2021 at 16:28)</a>:</h4>
<p>I think you're right (or at least very close).  I can build a really statically compiled binary working around the issue by using a docker container that does the job for me<br>
<a href="https://github.com/emk/rust-musl-builder">https://github.com/emk/rust-musl-builder</a></p>
<p>this container builds openssl (and other dependencies) internally, then builds your stuff.</p>



<a name="226537143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Trying%20out%20musl%20target/near/226537143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> apiraino <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Trying.20out.20musl.20target.html#226537143">(Feb 16 2021 at 16:29)</a>:</h4>
<p>it's a bit of a workaround but it seems to unblock me for the moment.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>